package com.zb.zeus.common.engine.domain;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zb.zeus.common.engine.Formula;
import com.zb.zeus.common.engine.annotation.FormulaDesc;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 计算常量
 * @author fujianjian
 * @since 2021/5/19 16:00
 */
public final class CalcConst {

    /**
     * 数据库删除字段 删除
     */
    public static final int DB_DELETED = 1;

    /**
     * 数据库删除字段 未删除
     */
    public static final int DB_NOT_DELETED = 0;

    /**
     * 数值1
     */
    public static final int ONE = 1;
    /**
     * 数值2
     */
    public static final int TWO = 2;


    /**
     * 数值10
     */
    public static final int THREE = 3;


    /**
     * 数值10
     */
    public static final int TEN = 10;

    /**
     * 逗号
     */
    public static final String COMMA = ",";

    /**
     * 等号
     */
    public static final String EQUAL_SIGN = "=";

    /**
     * 左括号
     */
    public static final String LEFT_BRACKETS = "(";

    /**
     * 右括号
     */
    public static final String RIGHT_BRACKETS = ")";

    /**
     * 左括号
     */
    public static final String LEFT_SQUARE_BRACKETS = "[";
    /**
     * 分号
     */
    public static final String SEMI = ";";

    /**
     * 小数点
     */
    public static final String POINT = "\\.";

    /**
     * 右括号
     */
    public static final String RIGHT_SQUARE_BRACKETS = "]";

    public static final String ORG_ID = "orgId";
    public static final String AD_NUM = "adNum";
    public static final String PERIOD = "period";
    public static final String CALC_STACK = "calcStack";

    public static final String CHECKED = "checked";
    public static final String SAME_LEVEL = "sameLevel";
    public static final String CUR_OBJ = "curObj";

    /**
     * 公式描述列表
     */
    public static final List<FormulaDescVO> FORMULA_DESC_LIST = Lists.newArrayList();

    /**
     * 初始化
     */
    static {
        Method[] methods = Formula.class.getDeclaredMethods();
        for (Method method : methods) {
            if (Modifier.isPublic(method.getModifiers())) {
                FormulaDescVO formulaVo = new FormulaDescVO();
                formulaVo.setFormula(method.getName());
                Class<?>[] parameterTypes = method.getParameterTypes();
                Parameter[] parameters = method.getParameters();
                Object[] args = new Object[parameters.length];
                for (int i = 0; i < parameters.length; i++) {
                    Map<String, String> params = Maps.newHashMap();
                    params.put(parameters[i].getName(), parameterTypes[i].getCanonicalName());
                    args[i] = params;
                }
                formulaVo.setArgs(args);
                formulaVo.setDesc(Optional.ofNullable(method.getAnnotation(FormulaDesc.class)).map(FormulaDesc::desc).orElse(""));
                FORMULA_DESC_LIST.add(formulaVo);
            }
        }
    }
}
